package com.twu.biblioteca.item;

import com.twu.biblioteca.user.User;

/**
 * Created by Enrico on 15/09/2014.
 */
public abstract class Item {

    protected String name;
    protected String author;
    protected int year;
    protected boolean available;

    public Item(String name, String author, int year) {
        this.name = name;
        this.author = author;
        this.year = year;
        this.available = true;
    }

    public abstract void showHeader();

    public abstract String formatted();

    public abstract void showCheckoutMessage();

    public abstract void showReturnMessage();

    public boolean matches(String name) {
        if (name == null) throw new NullPointerException(toString() + " cannot be null");
        return this.name.toLowerCase().equals(name.toLowerCase());
    }

    public void show() {
        if (available) System.out.println(formatted());
    }

    public void doCheckout(User user) {
        if (available) {
            available = false;
            user.add(this);
        }
    }

    public void doReturn(User user) {
        if (!available) {
            available = true;
            user.remove(this);
        }
    }

    public String getName() {
        return name;
    }

    public String getAuthor() {
        return author;
    }

    public int getYear() {
        return year;
    }

    public boolean isAvailable() {
        return available;
    }

    public void setAvailable(boolean available) {
        this.available = available;
    }
}
